﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.IO;
using System.Data;
using System.Transactions;

using Excel;
using Excel.Core;

public partial class AttendanceWebForm_Holiday : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            plQuery.DataBind();
            //YearCalendar1.DataBind();
        }
    }

    protected void YearCalendar1_SelectionChanged(object sender, EventArgs e)
    {
        var db = new DB("Attendance");

        using (TransactionScope scope = new TransactionScope())
        {
            // 判斷是否有此假日組別, 若無則新增
            var dr = db.ExecuteRow(string.Format("SELECT * FROM HolidayGroup WHERE {0}=@HolidayGroupName AND CompID=@CompID "
                , LocManager.Current.GetTextCol("HolidayGroup", "HolidayGroupName"))
                , new DBParameter("HolidayGroupName", txtHolidayGroupID.Text)
                , new DBParameter("CompID", OrgManager.Current.CurrentComp));

            int holidayGroupID;

            if (dr == null)
            {
                db.ExecuteNonQuery(string.Format(@"INSERT HolidayGroup (CompID, {0})
                                    SELECT @CompID, @HolidayGroupName"
                    , new DBParameter("CompID", OrgManager.Current.CurrentComp)
                    , new DBParameter("HolidayGroupName", txtHolidayGroupID.Text)));

                holidayGroupID = db.GetIdentity();
            }
            else
            {
                holidayGroupID = int.Parse(listHolidayGroupID.Text);
            }

            //db.ExecuteNonQuery(@"DELETE Holiday WHERE Year(Date) = @yy ", new DBParameter("yy", ddlYY.Text));

            foreach (DateTime d in YearCalendar1.CurrentAddDates)
            {
                Logger.WriteLog(d.ToString());
                db.ExecuteNonQuery(@"INSERT Holiday (HolidayGroupID, Date)
                                    SELECT @HolidayGroupID, @Date"
                    , new DBParameter("HolidayGroupID", holidayGroupID)
                    , new DBParameter("Date", d));
            }

            foreach (DateTime d in YearCalendar1.CurrentDeleteDates)
            {
                db.ExecuteNonQuery(@"DELETE Holiday WHERE HolidayGroupID=@HolidayGroupID AND Date=@Date"
                    , new DBParameter("HolidayGroupID", holidayGroupID)
                    , new DBParameter("Date", d));
            }

            scope.Complete();
        }        
    }

    protected void lknSample_Click(object sender, EventArgs e)
    {
        MyObj.DownloadFile("Sample.xlsx", @"~/AttendanceWebForm/Holiday/Sample.xlsx");
    }

    protected void btnImport_Click(object sender, EventArgs e)
    {


    }

    protected void btnQuery_Click(object sender, EventArgs e)
    {
        YearCalendar1.DataBind();
    }

    protected void btnBack_Click(object sender, EventArgs e)
    {
    }

    protected void gv_ItemCommand(object sender, GridViewCommandEventArgs e)
    {
    }

}